package com.itcast.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.dao.CheckItemDao;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.pojo.CheckItem;
import com.itcast.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查项服务
 */

@Service
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    //注入DAO对象
    @Autowired
    private CheckItemDao checkItemDao;
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    //分页
    @Override
    public PageResult PageQuery(QueryPageBean queryPageBean) {
        System.out.println(queryPageBean);
        System.out.println("22222222222222222222222222222");

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        //   Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
        Page<CheckItem> page = checkItemDao.selectByCondition(queryString);

        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();
        System.out.println(total);
        System.out.println(rows);
        System.out.println("3333333333333333333333333");
        return new PageResult(total,rows);
    }

    @Override
    public void deleteById(Integer id) {
        long itemId = checkItemDao.findCountByCheckItemId(id);
        System.out.println(itemId);
        if (itemId > 0){
            throw new RuntimeException();
        }
        System.out.println("222222222222222222222");
        checkItemDao.deleteById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        System.out.println("kkkkkkkkkkkkkkkkkkk");
        CheckItem byId = checkItemDao.findById(id);
        System.out.println(byId);
        return byId;
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemDao.findAll();
    }


}
